package com.ssyc.gsk_tk.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ssyc.common.base.LazyBaseFragment;
import com.ssyc.common.bean.BusInfo;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.view.MyListView;
import com.ssyc.gsk_tk.R;
import com.ssyc.gsk_tk.adapter.ReadChooseAnswerAdapter;
import com.ssyc.gsk_tk.adapter.ReadShowAnswerAdapter;
import com.ssyc.gsk_tk.bean.OverQuestion;
import com.ssyc.gsk_tk.bean.QuestionInfo;
import com.ssyc.gsk_tk.bean.ReadLvInfo;
import com.ssyc.gsk_tk.bean.SingleChooseInfo;
import com.ssyc.gsk_tk.util.TkKeys;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 单选题
 * Created by Administrator on 2018/9/3 0003.
 */

public class TkSingleChoiceFragment extends LazyBaseFragment {

    //页面对象
    private QuestionInfo.ListBean.ItemBean info;

    //页面的json数据
    private String jsonData;

    //单选的页面数据
    private SingleChooseInfo singleChooseInfo;


    //已完成的页面对象
    private OverQuestion.ListBean.ItemBean overinfo;

    /**
     * 选择答案
     */
    private TextView tv_content;
    private MyListView lv;


    //展示你的作答,正确答案,每个选项的正确率以及题目解析的相关视图
    //该视图只有在已完成状态下才显示出来
    private LinearLayout llShowAnswer;
    //你的答案
    private TextView tv_own_answer;
    //正确答案
    private TextView tv_right_answer;
    //展示选项正确率的ListView
    private MyListView lv_show_answer;
    //解析
    private TextView tv_parse_answer;

    //已完成状态下的解析和你的作答
    private String analysis;
    private String ownAnser;


    //该单选题的正确答案的标号
    private String rightAnswerIndex;

    private ReadChooseAnswerAdapter answerAdapter;

    //所有小题选项的正确率
    private List<OverQuestion.ListBean.ItemBean.ProbabilityBean> probability;

    private List<ReadLvInfo> oldDatas;

    public static final TkSingleChoiceFragment newInstance(QuestionInfo.ListBean.ItemBean item, OverQuestion.ListBean.ItemBean overData) {
        TkSingleChoiceFragment teacherGoldFragment = new TkSingleChoiceFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("data", item);
        bundle.putSerializable("overdata", overData);
        teacherGoldFragment.setArguments(bundle);
        return teacherGoldFragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tk_single_choice;
    }

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    public void lazyInit(View view, Bundle savedInstanceState) {

    }

    @Override
    protected void init(View view, Bundle savedInstanceState) {
        super.init(view, savedInstanceState);

        //显示内容
        showContent();

        //获取页面对象
        initIntent();

        //初始化视图
        initView(view);

        //设置数据
        initLv();

        //如果是已完成,初始化你的作答,正确答案,答案正确率和解析
        initHasCompetedData();
    }


    /**
     * 获取页面对象
     */
    private void initIntent() {

        //未完成的数据对象
        info = (QuestionInfo.ListBean.ItemBean) getArguments().getSerializable("data");

        //已完成的数据对象
        overinfo = (OverQuestion.ListBean.ItemBean) getArguments().getSerializable("overdata");

        if (info != null) {
            //获取页面的JSON数据
            jsonData = info.getContent();
            if (!TextUtils.isEmpty(jsonData)) {
                singleChooseInfo = GsonUtil.jsonToBean(jsonData, SingleChooseInfo.class);
            }
        } else if (overinfo != null) {
            //获取题目的解析
            analysis = overinfo.getAnalysis();

            //你的作答
            ownAnser = overinfo.getOption_content();

            //如果是已完成则解析overinfo
            jsonData = overinfo.getContent();
            if (!TextUtils.isEmpty(jsonData)) {
                singleChooseInfo = GsonUtil.jsonToBean(jsonData, SingleChooseInfo.class);
            }
            probability = overinfo.getProbability();
        }
    }


    /**
     * 初始化视图
     *
     * @param view
     */
    public void initView(View view) {
        tv_content = view.findViewById(R.id.tv_content);
        lv = view.findViewById(R.id.lv);
        if (null != singleChooseInfo) {
            tv_content.setText(singleChooseInfo.getContent());
        }
        llShowAnswer = view.findViewById(R.id.ll_show_answer);
        tv_own_answer = view.findViewById(R.id.tv_own_answer);
        tv_right_answer = view.findViewById(R.id.tv_right_answer);
        lv_show_answer = view.findViewById(R.id.lv_show_answer);
        tv_parse_answer = view.findViewById(R.id.tv_parse_answer);

        if (info != null) {
            //未完成
            lv.setEnabled(true);
            llShowAnswer.setVisibility(View.GONE);
        } else if (overinfo != null) {
            //已完成
            lv.setEnabled(false);
            llShowAnswer.setVisibility(View.VISIBLE);
        }
    }


    /**
     * 设置提供选择ListView的数据,如果是已完成的状态则ListView不可点击,同时数据模型会改变
     * 下面暂时模拟的是未完成的数据模型,可操作可点击
     */
    private void initLv() {

        if (singleChooseInfo != null) {
            //获取该题目的正确答案
            List<SingleChooseInfo.ListBean> listBeans = singleChooseInfo.getList();
            if (null != listBeans && listBeans.size() != 0) {
                for (int i = 0; i < listBeans.size(); i++) {
                    if ("true".equals(listBeans.get(i).getR())) {
                        rightAnswerIndex = listBeans.get(i).getL();
                        break;
                    }
                }
            }

            //创建需要展示的数据集合
            oldDatas = new ArrayList<>();
            if (null != listBeans && listBeans.size() != 0) {
                for (int i = 0; i < listBeans.size(); i++) {
                    ReadLvInfo info = new ReadLvInfo();
                    //设置编号
                    info.questionIndex = listBeans.get(i).getL();

                    //设置答案内容
                    info.questionContent = listBeans.get(i).getA();

                    if (overinfo != null) {
                        //已完成情况下给每个选项配置用户选择的答案以及正确答案
                        //配置用户选择的答案
                        info.chooseAnswer = ownAnser;

                        //给每个选项配置一个答案
                        info.rightAnswer = rightAnswerIndex;

                        if (probability != null && probability.size() != 0) {
                            if(!TextUtils.isEmpty(probability.get(0).getA())){
                                if(i==0){
                                    info.probability = probability.get(0).getA();
                                }
                            }
                            if(!TextUtils.isEmpty(probability.get(0).getB())){
                                if(i==1){
                                    info.probability = probability.get(0).getB();
                                }
                            }
                            if(!TextUtils.isEmpty(probability.get(0).getC())){
                                if(i==2){
                                    info.probability = probability.get(0).getC();
                                }
                            }
                            if(!TextUtils.isEmpty(probability.get(0).getD())){
                                if(i==3){
                                    info.probability = probability.get(0).getD();
                                }
                            }
                        }else{
                            info.probability = "100.0%";
                        }
                    } else if (info != null) {
                        //设置正确答案
                        if ("true".equals(listBeans.get(i).getR())) {
                            info.rightAnswer = listBeans.get(i).getA();
                        } else {
                            info.rightAnswer = "";
                        }
                    }
                    oldDatas.add(info);
                }
            }

            if (info != null) {
                //未完成状态
                answerAdapter = new ReadChooseAnswerAdapter(0, getContext(), oldDatas, R.layout.tk_lv_read_choose_answer);
                lv.setAdapter(answerAdapter);
                //设置条目点击事件
                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {

                        if (oldDatas.get(pos).isSelected) {
                            return;
                        } else {
                            //同步数据状态
                            answerAdapter.upDateStete(pos);
                            //保存选择的答案
                            //通过EventBs将用户作答本小题的相关信息传递给TkMainActivity保存
                            //currPos对应的每个大题的小题的位置,单选题默认就一个小题
                            int choosePos = 0;
                            //是否如正确
                            boolean chooseResult = false;
                            if (oldDatas.get(pos).rightAnswer.equals(oldDatas.get(pos).questionContent)) {
                                //用户作答正确
                                chooseResult = true;
                            } else {
                                //用户作答错误
                                chooseResult = false;
                            }
                            //回传当前提醒的TypeId,当前题型对应的是10
                            int typeId = 10;
                            BusInfo busInfo = new BusInfo();
                            busInfo.tkMsg = TkKeys.TKMsg;
                            busInfo.tkChoosePos = choosePos;
                            busInfo.tkChooseAnswer = oldDatas.get(pos).questionIndex;

                            busInfo.tkChooseResult = chooseResult;
                            busInfo.typeId = typeId;
                            EventBus.getDefault().post(busInfo);
                        }
                    }
                });

            } else if (overinfo != null) {
                //已完成状态
                answerAdapter = new ReadChooseAnswerAdapter(1, getContext(), oldDatas, R.layout.tk_lv_read_choose_answer);
                lv.setAdapter(answerAdapter);
            }
        }
    }


    /**
     * 初始化你的作答,正确答案,答案正确率和解析
     * 需要通过用户是否是已完成和未完成的状态来确定该部分的视图显示与隐藏,同时设置数据的数据源不一样
     * 该设置需要取已完成状态的数据源集合
     */
    private void initHasCompetedData() {
        //只有在已完成的页面对象不为null的情况下才设置已完成的而相关数据
        if (null != overinfo) {
            //设置展示每个选项的正确率列表
            ReadShowAnswerAdapter answerAdapter = new ReadShowAnswerAdapter(getContext(), oldDatas, R.layout.tk_lv_read_show_answer);
            lv_show_answer.setAdapter(answerAdapter);

            //设置你的选项答案,正确答案,和解析
            tv_own_answer.setText(ownAnser);


            if(ownAnser.equals(rightAnswerIndex)){
                tv_own_answer.setBackgroundResource(R.drawable.tk_tv_right_answer);
            }else{
                tv_own_answer.setBackgroundResource(R.drawable.tk_tv_own_choose_answer);
            }

            tv_right_answer.setText(rightAnswerIndex);
            tv_parse_answer.setText(analysis);
        }
    }
}

